﻿using System;
using System.Collections.Generic;
using System.Text;
using Common.Utilities;

namespace Workflows.Components.TextMining.Clustering
{
    /// <summary>
    /// 
    /// </summary>
    public class EuclideanDistanceCalculator : IDistanceCalculator
    {
        #region IDistanceCalculator Members
        /// <summary>
        /// 
        /// </summary>
        /// <param name="termFreq1"></param>
        /// <param name="termFreq2"></param>
        /// <param name="contributingTerms"></param>
        /// <returns></returns>
        public double CalculateDistance(
            Dictionary<int, DocTermFreq> termFreq1,
            Dictionary<int, DocTermFreq> termFreq2,
            ref Dictionary<int, int> contributingTerms)
        {
            if (termFreq1 == null || termFreq2 == null || termFreq1.Count == 0 || termFreq2.Count == 0)
                return double.PositiveInfinity;


            List<int> unionTermIDs = new List<int>();
            foreach (int termID1 in termFreq1.Keys)
            {
                unionTermIDs.Add(termID1);
            }
            foreach (int termID2 in termFreq2.Keys)
            {
                if (!unionTermIDs.Contains(termID2))
                {
                    unionTermIDs.Add(termID2);
                }
            }
            double sumSq = 0;
            foreach (int termID in unionTermIDs)
            {
                if (termFreq1.ContainsKey(termID) && termFreq2.ContainsKey(termID))
                {
                    sumSq += 0;
                    contributingTerms.Add(termID, Math.Min(termFreq1[termID].Count, termFreq2[termID].Count));
                }
                else
                {
                    int freq1 = 0;
                    if (termFreq1.ContainsKey(termID))
                        freq1 = termFreq1[termID].Count;
                    int freq2 = 0;
                    if (termFreq2.ContainsKey(termID))
                        freq2 = termFreq2[termID].Count;
                    sumSq += Math.Pow(freq1 - freq2, 2);
                }
            }
            double docLenScale = (double) contributingTerms.Count*2/
                                 (termFreq1.Count + termFreq2.Count - contributingTerms.Count);
            double euclideanDistance = Math.Sqrt(sumSq) * docLenScale;

            double contributingTermScale =
                TermWeightCalculator.TaminoCommonTermScale(
                    termFreq1.Count, termFreq2.Count,
                    contributingTerms.Count);
            return euclideanDistance*contributingTermScale;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="termFreq1"></param>
        /// <param name="termFreq2"></param>
        /// <param name="termWeights"></param>
        /// <param name="useNGramSimilarity"></param>
        /// <param name="contributingTerms"></param>
        /// <returns></returns>
        public double CalculateDistance(
            Dictionary<int, DocTermFreq> termFreq1,
            Dictionary<int, DocTermFreq> termFreq2,
            Dictionary<int, double> termWeights,
            bool useNGramSimilarity, int ngramLen,
            ref Dictionary<int, int> contributingTerms)
        {
            if (termFreq1 == null || termFreq2 == null || termFreq1.Count == 0 || termFreq2.Count == 0)
                return double.PositiveInfinity;

            List<int> unionTermIDs = new List<int>();
            foreach (int termID1 in termFreq1.Keys)
            {
                unionTermIDs.Add(termID1);
            }
            foreach (int termID2 in termFreq2.Keys)
            {
                if (!unionTermIDs.Contains(termID2))
                {
                    unionTermIDs.Add(termID2);
                }
            }
            double sumSq = 0;
            Dictionary<int, Dictionary<int, double>> termSimilarityMatrix =
                TermSimilarityMatrix.BuildTermDistanceMatrixUsingNgram(
                    termFreq1, termFreq2);

            foreach (int termID in unionTermIDs)
            {
                if (termFreq1.ContainsKey(termID) && termFreq2.ContainsKey(termID))
                {
                    sumSq += 0;
                    contributingTerms.Add(termID, Math.Min(termFreq1[termID].Count, termFreq2[termID].Count));
                }
                else
                {
                    double freq1 = 0;
                    if (termFreq1.ContainsKey(termID))
                        freq1 = termFreq1[termID].Count*termFreq1[termID].Weight;
                    double freq2 = 0;
                    if (termFreq2.ContainsKey(termID))
                        freq2 = termFreq2[termID].Count*termFreq2[termID].Weight;
                    if (useNGramSimilarity && ngramLen>0)
                    {
                        Dictionary<int, double> similarTerms = termSimilarityMatrix[termID];
                        foreach (int similarTermID in similarTerms.Keys)
                        {
                            if (termFreq1.ContainsKey(similarTermID) &&
                                termFreq1[similarTermID].Weight * termFreq1[similarTermID].Count > freq1)
                                freq1 = termFreq1[similarTermID].Weight*termFreq1[similarTermID].Count;
                            if (termFreq2.ContainsKey(similarTermID) &&
                                termFreq2[similarTermID].Weight * termFreq2[termID].Count > freq2)
                                freq2 = termFreq2[similarTermID].Weight*termFreq2[termID].Count;
                        }
                    }

                    sumSq += Math.Pow(freq1 - freq2, 2);
                }
            }
            double docLenScale = (double)contributingTerms.Count * 2 /
                                 (termFreq1.Count + termFreq2.Count - contributingTerms.Count);
            double euclideanDistance = Math.Sqrt(sumSq)*docLenScale;

            //Dictionary<int, double> contributingTermWeights = new Dictionary<int, double>();
            //foreach (int sharedTermID in contributingTerms.Keys)
            //{
            //    if (termWeights.ContainsKey(sharedTermID))
            //    {
            //        contributingTermWeights.Add(sharedTermID, termWeights[sharedTermID]);
            //    }
            //    else
            //    {
            //        contributingTermWeights.Add(sharedTermID, 0);
            //    }
            //}
            double contributingTermScale =
                TermWeightCalculator.TaminoWeightedTermScale(
                    termFreq1, termFreq2,
                    termWeights);

            return euclideanDistance*contributingTermScale;
        }

        public double CalculateDistance(
            Dictionary<int, DocTermFreq> termFreq1, 
            Dictionary<int, DocTermFreq> termFreq2,
            Dictionary<int, double> termWeights,
            Dictionary<int, string> termStemmings1,
            Dictionary<string,List<int>> termStemmings2,
            ref Dictionary<int, int> contributingTerms)
        {
            if (termFreq1 == null || termFreq2 == null || termFreq1.Count == 0 || termFreq2.Count == 0)
                return double.PositiveInfinity;

            List<int> unionTermIDs = new List<int>();
            foreach (int termID1 in termFreq1.Keys)
            {
                unionTermIDs.Add(termID1);
            }
            foreach (int termID2 in termFreq2.Keys)
            {
                if (!unionTermIDs.Contains(termID2))
                {
                    unionTermIDs.Add(termID2);
                }
            }
            double sumSq = 0;

            foreach (int termID in unionTermIDs)
            {
                if (termFreq1.ContainsKey(termID) && termFreq2.ContainsKey(termID))
                {
                    sumSq += 0;
                    contributingTerms.Add(termID, Math.Min(termFreq1[termID].Count, termFreq2[termID].Count));
                }
                else
                {
                    double freq1 = 0;
                    if (termFreq1.ContainsKey(termID))
                        freq1 = termFreq1[termID].Count * termFreq1[termID].Weight;
                    double freq2 = 0;
                    if (termFreq2.ContainsKey(termID))
                        freq2 = termFreq2[termID].Count * termFreq2[termID].Weight;
                    List<int> sharedTermIDs = termStemmings2[termStemmings1[termID]];
                    List<int> sharedTermIDs1=new List<int>();
                    List<int> sharedTermIDs2 = new List<int>();
                    foreach(int termID1 in termFreq1.Keys)
                    {
                        if(sharedTermIDs.Contains(termID1))
                        {
                            sharedTermIDs1.Add(termID1);
                        }
                    }
                    foreach(int termID2 in termFreq2.Keys)
                    {
                        if(sharedTermIDs.Contains(termID2))
                        {
                            sharedTermIDs2.Add(termID2);
                        }
                    }
                    if (sharedTermIDs1.Count>0 && sharedTermIDs2.Count>0)
                    {
                        foreach (int similarTermID in sharedTermIDs1)
                        {
                            freq1 = termFreq1[similarTermID].Weight * termFreq1[similarTermID].Count;
                        }
                        foreach(int similarTermID in sharedTermIDs2)
                        {
                            freq2 = termFreq2[similarTermID].Weight * termFreq2[termID].Count;
                        }
                    }

                    sumSq += Math.Pow(freq1 - freq2, 2);
                }
            }
            double docLenScale = (double)contributingTerms.Count * 2 /
                                 (termFreq1.Count + termFreq2.Count - contributingTerms.Count);
            double euclideanDistance = Math.Sqrt(sumSq) * docLenScale;

            //Dictionary<int, double> contributingTermWeights = new Dictionary<int, double>();
            //foreach (int sharedTermID in contributingTerms.Keys)
            //{
            //    if (termWeights.ContainsKey(sharedTermID))
            //    {
            //        contributingTermWeights.Add(sharedTermID, termWeights[sharedTermID]);
            //    }
            //    else
            //    {
            //        contributingTermWeights.Add(sharedTermID, 0);
            //    }
            //}
            double contributingTermScale =
                TermWeightCalculator.TaminoWeightedTermScale(
                    termFreq1, termFreq2,
                    termWeights);

            return euclideanDistance * contributingTermScale;
        }

        #endregion
    }
}
